package cn.sccl.common.controller;

import cn.sccl.common.model.CacheLog;
import cn.sccl.common.service.CacheLogManager;
import cn.sccl.common.web.BaseController;
import com.fasterxml.jackson.annotation.JsonView;
import cn.sccl.common.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 杨浩宇 on 2016-02-25.
 */
@RestController
@RequestMapping(value = "/common/cacheLog")
public class CacheLogController extends BaseController {

    @Autowired
    private CacheLogManager cacheLogManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<CacheLog> listAllCacheLog() {
        return cacheLogManager.listAll();
    }

    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<CacheLog> pageQueryCacheLog(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return cacheLogManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param cacheLog
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public CacheLog saveCacheLog(@RequestBody CacheLog cacheLog) {
        cacheLogManager.save(cacheLog);
        return cacheLog;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteCacheLog(@PathVariable("id") String id) {
        cacheLogManager.delete(id);
    }
}
